﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.Model;
using CustomsSystem.BLL;
using System.IO;
using Maticsoft.Common;
using System.IO;
using System.Text;

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;



namespace WEB.T_document
{
    public partial class Index_document : System.Web.UI.Page
    {
        //public DataSet ds;
        DataSet ds = new DataSet();
        DocumentManage DM = new DocumentManage();
        document model = new document();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");
            this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
            if (!Page.IsPostBack)
            {
                model.sheetID = Convert.ToInt32(Request.QueryString["ID"]);
                //默认时间为最近一月
                tbQuery.Text = " ";
                //DateTime A = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1);
                //int month = Convert.ToInt32(A.Date.Month.ToString());
                //tbTimestop.Text = A.ToShortDateString();
                //tbTimestart.Text = A.AddMonths(-2).Date.ToShortDateString();
                tbTimestop.Text = DateTime.Now.AddDays(1).ToShortDateString();
                tbTimestart.Text = DateTime.Now.AddMonths(-2).ToShortDateString();
                Bind();
            }
            if (gvDocument.Rows.Count == 0)
            {
                Label1.Text = "没有数据";
            }
            else
            {
                Label1.Text = "";
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvDocument_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int sheid = Convert.ToInt32(gvDocument.DataKeys[e.RowIndex].Value);
            int ID = Convert.ToInt32(e.RowIndex.ToString());
            DocumentManage DM = new DocumentManage();
            if (DM.Delete(sheid))
            {
                LinkButton b1 = (LinkButton)gvDocument.Rows[ID].Cells[1].Controls[0];
                string wj1 = b1.Text;
                if (wj1 != "" && wj1 != "0")
                {
                    string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj1);
                    FileInfo FD = new FileInfo(localPath);
                    FD.Delete();
                }
                #region
                //LinkButton b2 = (LinkButton)gvDocument.Rows[ID].Cells[4].Controls[0];
                //string wj2 = b2.Text;
                //if (wj2 != "" && wj2 != "0")
                //{
                //    string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj2);
                //    FileInfo FD = new FileInfo(localPath);
                //    FD.Delete();
                //}

                //LinkButton b3 = (LinkButton)gvDocument.Rows[ID].Cells[5].Controls[0];
                //string wj3 = b3.Text;
                //if (wj3 != "" && wj3 != "0")
                //{
                //    string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj3);
                //    FileInfo FD = new FileInfo(localPath);
                //    FD.Delete();
                //}

                //LinkButton b4 = (LinkButton)gvDocument.Rows[ID].Cells[6].Controls[0];
                //string wj4 = b4.Text;
                //if (wj4 != "" && wj4 != "0")
                //{
                //    string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj4);
                //    FileInfo FD = new FileInfo(localPath);
                //    FD.Delete();
                //}

                //LinkButton b5 = (LinkButton)gvDocument.Rows[ID].Cells[7].Controls[0];
                //string wj5 = b5.Text;
                //if (wj5 != "" && wj5 != "0")
                //{
                //    string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj5);
                //    FileInfo FD = new FileInfo(localPath);
                //    FD.Delete();
                //}
                #endregion
                Response.Write("<Script>alert('删除成功!')</script>");
                ds = DM.GetList2();

                gvDocument.DataSource = ds;
                gvDocument.DataBind();
            }

            else
                Response.Write("<Script>alert('删除失败!')</script>");

        }
        protected void gvDocument_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行         
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + "委托单号为" + e.Row.Cells[1].Text + "\"的全部数据和文件吗?请备份有关文件！')");
                }
            }
        }
        /// <summary>
        /// 压缩的方法
        /// </summary>
        #region
        ZipOutputStream zos = null;
                String strBaseDir = "";
                private void dlZipDir(string localpath, string strFileName)
                {
                    MemoryStream ms = null;
                    Response.ContentType = "application/octet-stream";
                    strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
                    Response.AddHeader("Content-Disposition", "attachment;   filename=" + strFileName + ".zip");
                    ms = new MemoryStream();
                    zos = new ZipOutputStream(ms);
                    strBaseDir = localpath + "\\";
                    addZipEntry(strBaseDir);
                    zos.Finish();
                    zos.Close();
                    Response.Clear();
                    Response.BinaryWrite(ms.ToArray());
                    Response.End();
                 }

             private  void addZipEntry(string PathStr)
               {
                   DirectoryInfo di = new DirectoryInfo(PathStr);
                   foreach (DirectoryInfo item in di.GetDirectories())
                   {
                       addZipEntry(item.FullName);
                   }
                   foreach (FileInfo item in di.GetFiles())
                   {
                                  FileStream fs = File.OpenRead(item.FullName);
                       byte[] buffer = new byte[fs.Length];
                       fs.Read(buffer, 0, buffer.Length);
                       string strEntryName = item.FullName.Replace(strBaseDir, "");
                       ZipEntry entry = new ZipEntry(strEntryName);
                       zos.PutNextEntry(entry);
                       zos.Write(buffer, 0, buffer.Length);
                       fs.Close();
                   }
              
       



                }
             #endregion
             //public void ZipFileMain(string[] filenames, string name, int Level)下载的方法
                 //{
                 //    ZipOutputStream s = new ZipOutputStream(File.Create(name));
                 //    Crc32 crc = new Crc32();
                 //    //压缩级别
                 //    s.SetLevel(Level); // 0 - store only to 9 - means best compression
                 //    try
                 //    {
                 //        foreach (string file in filenames)
                 //        {
                 //            //打开压缩文件
                 //            FileStream fs = File.OpenRead(file.Split('|')[0]);//文件地址
                 //            byte[] buffer = new byte[fs.Length];
                 //            fs.Read(buffer, 0, buffer.Length);
                 //            //建立压缩实体
                 //            ZipEntry entry = new ZipEntry(file.Split('|')[1]);//原文件名
                 //            //时间
                 //            entry.DateTime = DateTime.Now;
                 //            //空间大小
                 //            entry.Size = fs.Length;
                 //            fs.Close();
                 //            crc.Reset();
                 //            crc.Update(buffer);
                 //            entry.Crc = crc.Value;
                 //            s.PutNextEntry(entry);
                 //            s.Write(buffer, 0, buffer.Length);
                 //        }
                 //    }
                 //    catch
                 //    {
                 //        throw;
                 //    }
                 //    finally
                 //    {
                 //        s.Finish();
                 //        s.Close();
                 //    }
                 //}

        /// <summary>
        /// 下载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvDocument_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           
            int index = Convert.ToInt32(e.CommandArgument.ToString());
            string b1 = "";
            int a;
            if (e.CommandName == "download1")
            {
                b1 = gvDocument.Rows[index].Cells[2].Text; 
                int sheetID =Convert.ToInt32( gvDocument.Rows[index].Cells[2].Text);//获取sheetid
                string localPath = Server.MapPath("~/document/" + "T_document" + "/" + sheetID);//文件路径
                dlZipDir(localPath,sheetID.ToString());
                // LinkButton b1 = (LinkButton)gvDocument.Rows[index].Cells[0].Controls[0];
                string wj1 = b1;
                a = Convert.ToInt32(b1);
                model = DM.GetModel(a);//a为sheetid
                if (wj1 != "" && wj1 != "0")
                { 
                     FileInfo fi1 = new FileInfo(localPath );
                     if (fi1.Exists == true)
                     {
                         Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码     “http://blog.csdn.net/ciznx/article/details/5625222”；
                         Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi1.Name)); //解决中文文件名乱码   
                         Response.AddHeader("Content-length", fi1.Length.ToString());
                         Response.ContentType = "appliction/octet-stream";
                         Response.WriteFile(localPath );
                         Response.Flush();
                         Response.Clear();
                     }
                }

                #region
                // FileInfo fi1 = new FileInfo(localPath + wj1);
                    // if (wj1 != "" && wj1 != "0")
                    //LinkButton b = (LinkButton)gvDocument.Rows[index].Cells[0].FindControl("DownLoad1");
                    //Response.Redirect("~/download.aspx");
        //            if (model.docName1 != "" && model.docName1 != null && model.docName1.Trim() != "没有上传")
        //            {
        //                FileInfo fi1 = new FileInfo(localPath + model.docName1);
        //                if (fi1.Exists == true)
        //                {
        //                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码     “http://blog.csdn.net/ciznx/article/details/5625222”；
        //                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi1.Name)); //解决中文文件名乱码   
        //                    Response.AddHeader("Content-length", fi1.Length.ToString());
        //                    Response.ContentType = "appliction/octet-stream";
        //                    Response.WriteFile(localPath + model.docName1);
        //                    Response.Flush();
        //                    Response.Clear();
        //                }
        //                else
        //                {

        //                    Response.Write("<script>alert('文件已经丢失!')</script>");

        //                }
        //            }
        //            else
        //            {

        //                Response.Write("<script>alert('没有该文件下载!')</script>");

        //            }
        //    }

        //    if (e.CommandName == "download2")
        //    {
        //        b1 = gvDocument.Rows[index].Cells[5].Text;

        //        string wj2 = b1;
        //        a = Convert.ToInt32(b1);
        //        model = DM.GetModel(a);


        //        if (wj2 != "" && wj2 != "0")
        //        {

        //            if (model.docName2 != "" && model.docName2 != null)
        //            {
        //                FileInfo fi1 = new FileInfo(localPath + model.docName2);
        //                if (fi1.Exists == true)
        //                {
        //                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码    
        //                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi1.Name)); //解决中文文件名乱码   
        //                    Response.AddHeader("Content-length", fi1.Length.ToString());
        //                    Response.ContentType = "appliction/octet-stream";
        //                    Response.WriteFile(localPath + model.docName2);
        //                    Response.Flush();
        //                    Response.Clear();
        //                    Response.End();
        //                }
        //            }
        //            else
        //            {

        //                Response.Write("<script>alert('文件已经丢失')</script>");

        //            }

        //        }
        //        else
        //        {
        //            Response.Write("<script>alert('没有该文件下载!')</script>");

        //        }
        //    }

        //    if (e.CommandName == "download3")
        //    {
        //        b1 = gvDocument.Rows[index].Cells[5].Text;

        //        string wj3 = b1;
        //        a = Convert.ToInt32(b1);
        //        model = DM.GetModel(a);


        //        if (wj3 != "" && wj3 != "0")
        //        {

        //            if (model.docName3 != "" && model.docName3 != null)
        //            {
        //                FileInfo fi1 = new FileInfo(localPath + model.docName3);
        //                if (fi1.Exists == true)
        //                {
        //                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码    
        //                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi1.Name)); //解决中文文件名乱码   
        //                    Response.AddHeader("Content-length", fi1.Length.ToString());
        //                    Response.ContentType = "appliction/octet-stream";
        //                    Response.WriteFile(localPath + model.docName3);
        //                    Response.Flush();
        //                    Response.Clear();
        //                    Response.End();
        //                }
        //            }
        //            else
        //            {

        //                Response.Write("<script>alert('文件已经丢失,没有该文件下载!')</script>");

        //            }

        //        }
        //        else
        //        {
        //            Response.Write("<script>alert('没有该文件下载!')</script>");

        //        }
        //    }
        //    if (e.CommandName == "download4")
        //    {
        //        b1 = gvDocument.Rows[index].Cells[5].Text;

        //        string wj4 = b1;
        //        a = Convert.ToInt32(b1);
        //        model = DM.GetModel(a);


        //        if (wj4 != "" && wj4 != "0")
        //        {

        //            if (model.docName4 != "" && model.docName4 != null)
        //            {
        //                FileInfo fi1 = new FileInfo(localPath + model.docName4);
        //                if (fi1.Exists == true)
        //                {
        //                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码    
        //                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi1.Name)); //解决中文文件名乱码   
        //                    Response.AddHeader("Content-length", fi1.Length.ToString());
        //                    Response.ContentType = "appliction/octet-stream";
        //                    Response.WriteFile(localPath + model.docName4);
        //                    Response.Flush();
        //                    Response.Clear();
        //                    Response.End();
        //                }
        //            }
        //            else
        //            {

        //                Response.Write("<script>alert('文件已经丢失,没有该文件下载!')</script>");

        //            }

        //        }
        //        else
        //        {
        //            Response.Write("<script>alert('没有该文件下载!')</script>");

        //        }

       #endregion
           }
        }
     
        protected void Bind()
        {

            ds = DM.GetList1(" " + ddlQuery.SelectedItem.Value + "  like '%" + tbQuery.Text.Trim() + "%'", tbTimestart.Text, tbTimestop.Text);
            gvDocument.DataSource = ds.Tables[0];
            gvDocument.DataBind();

        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void gvDocument_DataBound(object sender, EventArgs e)
        {
            for (int i = 0; i < gvDocument.Rows.Count; i++)
            {
               // gvDocument.Rows[i].Cells[0].Text = "";
            }

            if (gvDocument.Rows.Count == 0)
            {
                Label1.Text = "没有数据";
            }
            else
            {
                Label1.Text = "";
            }
        }


    }
   
    
    
}

        //protected void gvDocument_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        //{
        //    int sheid = Convert.ToInt32(gvDocument.DataKeys[e.RowIndex].Value);
        //    int ID = Convert.ToInt32(e.RowIndex.ToString());
        //    DocumentManage DM = new DocumentManage();
        //    if (DM.Delete(sheid))
        //    {
        //        LinkButton b1 = (LinkButton)gvDocument.Rows[ID].Cells[3].Controls[0];
        //        string wj1 = b1.Text;
        //        if (wj1 != "" && wj1 != "0")
        //        {
        //            string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj1);
        //            FileInfo FD = new FileInfo(localPath);
        //            FD.Delete();
        //        }

        //        LinkButton b2 = (LinkButton)gvDocument.Rows[ID].Cells[4].Controls[0];
        //        string wj2 = b2.Text;
        //        if (wj2 != "" && wj2 != "0")
        //        {
        //            string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj2);
        //            FileInfo FD = new FileInfo(localPath);
        //            FD.Delete();
        //        }

        //        LinkButton b3 = (LinkButton)gvDocument.Rows[ID].Cells[5].Controls[0];
        //        string wj3 = b3.Text;
        //        if (wj3 != "" && wj3 != "0")
        //        {
        //            string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj3);
        //            FileInfo FD = new FileInfo(localPath);
        //            FD.Delete();
        //        }
        //        LinkButton b4 = (LinkButton)gvDocument.Rows[ID].Cells[6].Controls[0];
        //        string wj4 = b4.Text;
        //        if (wj4 != "" && wj4 != "0")
        //        {
        //            string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj4);
        //            FileInfo FD = new FileInfo(localPath);
        //            FD.Delete();
        //        }
        //        LinkButton b5 = (LinkButton)gvDocument.Rows[ID].Cells[7].Controls[0];
        //        string wj5 = b5.Text;
        //        if (wj5 != "" && wj5 != "0")
        //        {
        //            string localPath = Server.MapPath("~/document/" + "T_document" + "/" + wj5);
        //            FileInfo FD = new FileInfo(localPath);
        //            FD.Delete();
        //        }

        //        Response.Write("<Script>alert('删除成功!')</script>");

        //        ds = DM.GetList();
        //        gvDocument.DataSource = ds;
        //        gvDocument.DataBind();
        //    }

        //    else
        //        Response.Write("<Script>alert('删除失败!')</script>");

        //}
    
